from PyQt6.QtCore import QThread, pyqtSignal


class SerialThread(QThread):
    data_received = pyqtSignal(bytes)

    def __init__(self, serial_port):
        super().__init__()
        self.serial_port = serial_port
        self._running = True

    def run(self):
        while self._running and self.serial_port.is_open:
            if self.serial_port.in_waiting:
                data = self.serial_port.read(self.serial_port.in_waiting)
                self.data_received.emit(data)

    def stop(self):
        self._running = False
        self.wait()
